/**
 * 时间格式->时间戳
 */
export function transdate(time) {
  let date = new Date();
  date.setFullYear(time.substring(0, 4));
  date.setMonth(time.substring(5, 7) - 1);
  date.setDate(time.substring(8, 10));
  date.setHours(time.substring(11, 13));
  date.setMinutes(time.substring(14, 16));
  date.setSeconds(time.substring(17, 19));
  return Date.parse(date) / 1000;
}

/*时间戳过滤为日期格式
@param time 时间戳
@param cFormat 日期格式（默认'{y}-{m}-{d} {h}:{i}:{s}'）
*/
export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null;
  }

  if ((time + "").length === 10) {
    time = +time * 1000;
  }

  const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
  let date;
  if (typeof time === "object") {
    date = time;
  } else {
    date = new Date(parseInt(time, 10));
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  };
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key];
    if (key === "a")
      return ["一", "二", "三", "四", "五", "六", "日"][value - 1];
    if (result.length > 0 && value < 10) {
      value = "0" + value;
    }
    return value || 0;
  });
  return time_str;
}
/*时间戳过滤为日期格式，距当前多少时间差
@param time 时间戳
@param option 是否自定义日期格式
*/
export function formatTime(time, option) {
  time = +time * 1000;
  const d = new Date(time);
  const now = Date.now();

  const diff = (now - d) / 1000;

  if (diff < 30) {
    return "刚刚";
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + "分钟前";
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + "小时前";
  } else if (diff < 3600 * 24 * 2) {
    return "1天前";
  }
  if (option) {
    return parseTime(time, option);
  } else {
    return (
      d.getMonth() +
      1 +
      "月" +
      d.getDate() +
      "日" +
      d.getHours() +
      "时" +
      d.getMinutes() +
      "分"
    );
  }
}
/**
 * 标准时间转时间格式
 * @param date 标准时间
 * @param fmt  转换格式
 */
export function tzFormatTime(date, fmt) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
    let o = {
      "M+": date.getMonth() + 1,
      "d+": date.getDate(),
      "h+": date.getHours(),
      "m+": date.getMinutes(),
      "s+": date.getSeconds()
    };
    for (let k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
        let str = o[k] + "";
        fmt = fmt.replace(
          RegExp.$1,
          RegExp.$1.length === 1 ? str : ("00" + str).substr(str.length)
        );
      }
    }
    return fmt;
  }
}
//日期格式把秒去掉
export function cutDateSecond(date) {
  if (date) {
    return date.slice(0, -3);
  }
}
//比较两个日期大小
export function compareDate(date1, date2) {
  var oDate1, oDate2;
  if (date1.indexOf("-") == -1 && date2.indexOf("-") == -1) {
    oDate1 = new Date("2020-01-01 " + date1);
    oDate2 = new Date("2020-01-01 " + date2);
  } else {
    oDate1 = date1;
    oDate2 = date2;
  }
  if (oDate1.getTime() > oDate2.getTime()) {
    return true;
  } else {
    return false;
  }
}
